<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/13
 * Time: 23:51
 */

namespace App\Models\Api;

use App\Models\Api\Base;
use Cache;

/**
 * 文章点赞模型
 * Class ArticleGood
 * @package app\common\mode
 */
class AstroSignText extends Base
{
    protected $table = 'astro_sign_text';

    public function getAllHouseText($map = [])
    {
        $house_text = false;

        if (intval($this->cache_on) >= 1 && Cache::has('astro_house_text'))
        {
            $house_text = Cache::get('astro_house_text');
        }
        if ($house_text === false)
        {
            try
            {
                $house_text = $this->select('tid','planet','text')
                    ->where(['type' => 1, 'status' => 1])
                    ->get();

                $house_text = json_decode(json_encode($house_text, true), true);

                if ($this->cache_on >= 1)
                {
                    Cache::set('astro_house_text', $house_text, 86400 * 3);
                }
            }
            catch (\Exception $e)
            {
                $house_text = [];
            }
        }
        return $house_text;
    }

    public function getHouseText($map = [])
    {
        $planet_id = $map['planet_id'];
        $house_id = $map['house_id'];

        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has('astro_house_text' . $planet_id . '-' . $house_id))
        {
            $data = Cache::get('astro_house_text' . $planet_id . '-' . $house_id);
        }

        if ($data === false)
        {
            try{
                $house_text = $this->select('text')
                    ->where(['status' => 1, 'type' => 1, 'tid' => $house_id, 'planet' => $planet_id])
                    ->get();

                $house_text = json_decode(json_encode($house_text, true), true);

                if ($house_text || !empty($house_text))
                {
                    foreach ($house_text as $text)
                    {
                        $data[] = $text['text'];
                    }
                }
                if ($this->cache_on >= 1)
                {
                    Cache::set('astro_house_text' . $planet_id . '-' . $house_id, $data, 86400 * 3);
                }
            }
            catch (\Exception $e)
            {
                $data = [];
            }
        }
        return $data;
    }
}